<?php

namespace util;
class LeftNav
{
    static public function category($cate, $leftHtml = '|— ', $pid = 0, $level = 0, $leftPin = 0)
    {
        $arr = [];
        foreach ($cate as $val) {
            if ($val['pid'] == $pid) {
                $val['level'] = $level + 1;
                $val['leftHtml'] = str_repeat($leftHtml, $level);
                $val['leftTitle'] = $val['leftHtml'] . $val['title'];
                $arr[] = $val;
                $arr = array_merge($arr, self::category($cate, $leftHtml, $val['id'], $level + 1, $leftPin + 20));
            }
        }

        return $arr;
    }

    static public function toOptionArray($arr, $key = 'id', $value = 'leftTitle', $extra = '')
    {
        $options = [];
        if ($extra) {
            $options[0] = $extra;
        }
        foreach ($arr as $val) {
            $options[$val[$key]] = $val[$value];
        }
        return $options;
    }
}
